<script lang="ts" setup>
import { reactive, onActivated, defineAsyncComponent } from 'vue'
import UserInfoForm from '@/components/form/UserInfoForm.vue'
import { useRoute } from 'vue-router'
import { useActivated } from '@/hooks/useActivated'
import {
  bikeModifieldsDetailOrderTableCol,
  queryUserInfoDetailOrderRequestData,
  queryUserInfoDetailOrderTableCol
} from '@/api/user/types/user'
import { queryUserInfoDetailOrder } from '@/api/user'
import { useTable } from '@/hooks/useTable'
import { getToken } from '@/utils/cache/cookies'
defineOptions({
  name: 'BikeModifieldsOrder'
})
const BikeModifieldsTable = defineAsyncComponent(
  () => import('@/components/table/BikeModifieldsTable.vue')
)
const route = useRoute()
const form = reactive<queryUserInfoDetailOrderRequestData>({
  phone: route.query.phone as string,
  orderId: route.query.orderId as string,
  type: '',
  selectType: 'bikeModified'
})
const handleFormSearch = () => {
  setCurrentPage(1)
  load()
}
const { pagination, handleSizeChange, handleCurrentChange, data, loading, setCurrentPage, load } =
  useTable<queryUserInfoDetailOrderTableCol>(queryUserInfoDetailOrder, getToken() ?? '', () => form)
defineExpose({ load })
/** 处理是否缓存 */
onActivated(() => {
  form.phone = (route.query.phone as string) || ''
  form.orderId = (route.query.orderId as string) || ''
  form.selectType = (route.query.selectType as string) || ''
  form.type = (route.query.type as string) || ''
})
useActivated(
  form,
  { form },
  [
    { key: 'phone', path: 'form.phone' },
    { key: 'selectType', path: 'form.selectType' },
    { key: 'type', path: 'form.type' },
    { key: 'orderId', path: 'form.orderId' }
  ],
  load,
  []
)
</script>

<template>
  <div class="xy-container">
    <user-info-form v-model="form" type="bikemodifield" @search="handleFormSearch">
      <template #select>
        <el-form-item prop="type" label="订单状态">
          <el-select v-model="form.type" style="width: 150px">
            <el-option value="" label="不限" />
            <el-option value="1" label="待安装" />
            <el-option value="2" label="已安装" />
            <el-option value="3" label="已取消" />
          </el-select>
        </el-form-item>
      </template>
    </user-info-form>
    <bike-modifields-table :loading="loading" :data="data as bikeModifieldsDetailOrderTableCol[]" />
    <div flex justify-end mt-20>
      <el-pagination
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        v-model:current-page="pagination.pageNum"
        v-model:page-size="pagination.pageSize"
        background
        :total="pagination.total"
        layout="total,prev,pager,next,jumper,sizes"
      />
    </div>
  </div>
</template>

<style scoped></style>
